/* global window */
import modelExtend from 'dva-model-extend'
import * as customerService from '../../services/customer'
import * as personalChatRecordsService from '../../../personalChatRecords/services/personalChatRecords'
import * as dictionaryService from 'services/dictionary'
import * as departmentService from '../../../department/services/departments'
import * as roleService from '../../../role/services/roles'
import {pageModel} from 'utils/model'
import pathToRegexp from 'path-to-regexp'
import {message} from 'antd'


const {queryById, update, save, queryAddress, saveAddr, updateAddr, delAddr} = customerService

export default modelExtend(pageModel, {
  namespace: 'customerDetail',

  state: {
    disabled: true,
    data: {},
    friendType: [],
    adAcquireType: [],
    departments: [],
    roles: [],
    selectedRowKeys: [],
    selectRecord: {},
    list: [],
    addressList: [],
    modalVisible: false,
    addrModalVisible: false,
    currentAddr: {},
    addrModalType: 'create',
  },

  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({pathname, query}) => {
        const match = pathToRegexp('/customer/:id').exec(pathname)
        if (match) {
          dispatch({type: 'query', payload: {id: match[1]}})
          dispatch({
            type: 'findCustomerChatRecords',
            payload: {current: 1, size: 10, cid: match[1], owxId: query.owxId},
          })
          dispatch({type: 'init'})
          dispatch({type: "employee/query"})
        }
      })
    },
  },

  effects: {

    * findCustomerChatRecords ({payload = {}}, {call, put}) {
      const response = yield call(personalChatRecordsService.findCustomerChatRecords, payload)
      const {data} = response
      if (response && data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.records,
            pagination: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.total,
            },
          },
        })
      } else {
        throw response
      }
    },

    * query ({payload = {}}, {call, put}) {
      const response = yield call(queryById, payload)
      const {data} = response
      if (response && response.code === 0 && data) {
        yield put({type: 'updateState', payload: {data}})
      } else {
        throw data
      }
    },

    * queryAddress ({payload = {}}, {call, put}) {
      const response = yield call(queryAddress, payload)
      const {data} = response
      if (response && response.code === 0 && data) {
        yield put({
          type: 'updateState',
          payload: {
            addressList: data,
            addressPage: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.total,
            },
          },
        })
      } else {
        throw data
      }
    },

    * init (_, {call, put}) {
      const friendTypeResp = yield call(dictionaryService.queryBySn, {sn: 'friend_type'})
      const adAcquireTypeResp = yield call(dictionaryService.queryBySn, {sn: 'customer_channel'})
      const deptResp = yield call(departmentService.query)
      const roleResp = yield call(roleService.query)

      yield put({
        type: 'updateState', payload: {
          friendType: friendTypeResp.data,
          adAcquireType: adAcquireTypeResp.data,
          departments: deptResp.data,
          roles: roleResp.data.records,
        },
      })
    },

    * update ({payload}, {call, put}) {
      const {data, code, msg} = yield call(update, payload)
      if (code === 0) {
        message.success(msg)
        yield put({
          type: 'updateState',
          payload: {
            disabled: true,
          },
        })
      } else {
        message.error(msg)
        throw msg
      }
    },

    * updateAddr ({payload}, {call, put}) {
      const {code, msg} = yield call(updateAddr, payload)
      if (code === 0) {
        message.success(msg)
        yield put({type: "hideAddrModal"})
      } else {
        message.error(msg)
        throw msg
      }
    },

    * save ({payload}, {call, put}) {
      const {code, msg} = yield call(save, payload)
      if (code === 0) {
        message.success(msg)
        yield put({type: "hideModal"})
      } else {
        message.error(msg)
      }
    },

    * saveAddr ({payload}, {call, put}) {
      const {code, msg} = yield call(saveAddr, payload)
      if (code === 0) {
        message.success(msg)
        yield put({type: "hideAddrModal"})
      } else {
        message.error(msg)
      }
    },

    * delAddr ({payload}, {call}) {
      const {code, msg} = yield call(delAddr, payload)
      if (code === 0) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },
  },

  reducers: {
    updatePagination (state, {payload}) {
      let pagination = {
        ...state.pagination,
        current: Number(payload.current) || state.pagination.current,
        pageSize: Number(payload.size) || state.pagination.pageSize,
      }
      return {...state, pagination}
    },

    hideModal (state) {
      return {...state, modalVisible: false}
    },

    hideAddrModal (state) {
      return {...state, addrModalVisible: false}
    },

    showAddrModal (state, {payload}) {
      return {...state, ...payload, addrModalVisible: true}
    },
  },
})
